package com.imooc.controller;

import com.imooc.dataobject.ProductCategory;
import com.imooc.dataobject.ProductInfo;
import com.imooc.dataobject.VO.ProductCategoryVO;
import com.imooc.dataobject.VO.ProductInfoVo;
import com.imooc.dataobject.VO.RequestVO;
import com.imooc.service.ProductCategoryService;
import com.imooc.service.ProductInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @param
 * @author chenmc
 * @date $date$ $time$
 * @return $returns$
 */
@RestController
@RequestMapping("/buyer/product")
public class BuyerProductController {
    @Autowired
    private ProductInfoService productInfoService;

    @Autowired
    private ProductCategoryService  productCategoryService;

    @RequestMapping("/list")
    public RequestVO<ProductCategoryVO> getList(){
        List<ProductCategoryVO> data = new ArrayList<>();
        RequestVO<ProductCategoryVO> requestVO = new RequestVO<>();
        //所有的上架商品
        List<ProductInfo> allUp = productInfoService.findAllUp();
        //存放所有上架的商品的类型
        List<Integer> listCategory = new ArrayList<>();

        List<ProductCategory> allproductCategory = productCategoryService.getProductCategoryInCategory(listCategory);

        for (ProductInfo productInf:allUp){
            listCategory.add(productInf.getCategoryType());
        }


        for (ProductCategory productCategory:allproductCategory){
            ProductCategoryVO productCategoryVO = new ProductCategoryVO();
            List<ProductInfoVo> productInfoVos  = new ArrayList<>();

            productCategoryVO.setCategoryName(productCategory.getCategoryName());
            productCategoryVO.setCateGoryType(productCategory.getCategoryType());
            for (ProductInfo productInfo: allUp){
                if (productInfo.getCategoryType()==productCategory.getCategoryType()){
                    ProductInfoVo productInfoVo = new ProductInfoVo();
                    BeanUtils.copyProperties(productInfo,productInfoVo);
                    productInfoVos.add(productInfoVo);
                }


            }

            productCategoryVO.setFoods(productInfoVos);
            data.add(productCategoryVO);
        }

        requestVO.setData(data);
      return  requestVO;
    }



}
